import app
from api.login_api import LoginAPI
import requests

from app import BASE_URL, HEADERS


class Employee(object):
    def __init__(self):
        # # 获取登录token令牌
        # self.token = "Bearer " + LoginAPI().login("13800000002", "123456").json().get("data")
        # # 请求头
        # HEADERS["Authorization"] = self.token
        # 查询员工列表url
        self.check_emp_list_url = BASE_URL + "/api/sys/user?page=1&size=10"
        # 添加员工url
        self.check_emp_add_url = BASE_URL + "/api/sys/user"
        # 查询员工url
        # 修改员工url
        # 删除员工url
        self.check_emp_del_url = self.check_emp_check_url = self.check_emp_modify_url \
            = BASE_URL + "/api/sys/user/"

    def check_emp_list(self, headers):
        response = requests.get(url=self.check_emp_list_url, headers=headers)
        return response

    def add_emp(self, jsonData, headers):
        response = requests.post(url=self.check_emp_add_url, headers=headers, json=jsonData)
        return response

    def check_emp(self, id, headers):
        response = requests.get(url=self.check_emp_check_url + id, headers=headers)
        return response

    def modify_emp(self, id, jsonData, headers):
        response = requests.put(url=self.check_emp_check_url + id, headers=headers, json=jsonData)
        return response

    def del_emp(self, id, headers):
        response = requests.delete(url=self.check_emp_check_url + id, headers=headers)
        return response


if __name__ == '__main__':
    em = Employee()
    login = LoginAPI()
    token = login.login("13800000002", "123456").json().get("data")
    HEADERS["Authorization"] = "Bearer " + token
    headers = HEADERS
    print("请求头:", headers)
    # 查询员工列表
    print("员工列表：", em.check_emp_list(headers).json())
    # 添加员工并提取ID
    re = em.add_emp(
        {"username": "尼古拉斯特斯拉", "mobile": "18123336831", "workNumber": "001", "timeOfEntry": "2020-01-01",
         "forOfEmployement": "1", "departmentId": "9993", "departmentName": "深圳18期测试部",
         "correctionTime": "2020-01-01"}, headers)
    print("添加员工:", re.json())
    emp_id = re.json().get("data").get("id")
    app.EMP_ID = emp_id
    print(app.EMP_ID)
    # 查询员工
    print("查询员工：", em.check_emp(app.EMP_ID, headers).json())
    # 修改员工
    print("修改员工：", em.modify_emp(app.EMP_ID, {"username": "董若萱"}, headers).json())
    # 删除员工
    print("删除员工：", em.del_emp(app.EMP_ID, headers).json())
